<template>
  <Page v-slot:default="{contentClass}" :show-nav-bar="false" style="height: 100vh">
    <div :class="contentClass" class="flex justify-center items-center h-full">
      <van-button type="primary" round @click="handleLogin">微信登录</van-button>
    </div>
  </Page>
</template>

<script setup>
import {ref} from 'vue';
import {useStore} from "vuex";
import {useRoute, useRouter} from "vue-router";
import Page from "../components/Page.vue"
import {computed, onMounted, watch} from "vue";

const store = useStore()
const router = useRouter()
const route = useRoute()

const handleLogin = async () => {
  localStorage.setItem('authCount', 0) // redirect reset
  await store.dispatch('user/wxAuth') // direct auth url
}

onMounted(async () => {

  if (store.getters["user/token"]) { // Exist token then get user.
    await store.dispatch("user/getUser")
  } else if (route.query.code) { // Exist code then code to login.
    await store.dispatch("user/wxAuth", route.query.code)
  }

  const user = store.getters["user/user"]
  if (user) {
    const backPage = localStorage.getItem('backPage')
    console.log(backPage)
    console.log(backPage || '/')
    await router.replace(backPage || '/')
  }

})
</script>

<style scoped>

</style>